/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CategoriesDialog.java
 *
 * Created on Apr 13, 2011, 12:57:13 PM
 */
package homebudget;

import javax.swing.DefaultListModel;

/**
 *
 * @author Krit
 */
public class CategoriesDialog extends javax.swing.JDialog {

   

    /** Creates new form CategoriesDialog */
    public CategoriesDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);

        initComponents();

        DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());
        expensesCategoriesList.setListData(baza.listaKategoriaWydatek().toArray());
        contributionCategoriesList.setListData(baza.listaKategoriaPrzychod().toArray());
        productCategoriesList.setListData(baza.listaKategoriaProdukt().toArray());


        deleteCategoryButton.setVisible(false);
        btnEdytuj.setVisible(false);
        jTextFieldEdytuj.setVisible(false);
    }



    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        categories = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        expensesCategoriesList = new javax.swing.JList();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        contributionCategoriesList = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        productCategoriesList = new javax.swing.JList();
        newCategoryTextField = new javax.swing.JTextField();
        addCategoryButton = new javax.swing.JButton();
        deleteCategoryButton = new javax.swing.JButton();
        btnEdytuj = new javax.swing.JButton();
        jTextFieldEdytuj = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(homebudget.HomeBudgetApp.class).getContext().getResourceMap(CategoriesDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        categories.setName("categories"); // NOI18N
        categories.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                categoriesStateChanged(evt);
            }
        });

        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        expensesCategoriesList.setName("expensesCategoriesList"); // NOI18N
        expensesCategoriesList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                expensesCategoriesListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(expensesCategoriesList);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
        );

        categories.addTab(resourceMap.getString("jPanel1.TabConstraints.tabTitle"), jPanel1); // NOI18N

        jPanel2.setName("jPanel2"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        contributionCategoriesList.setName("contributionCategoriesList"); // NOI18N
        contributionCategoriesList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                contributionCategoriesListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(contributionCategoriesList);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
        );

        categories.addTab(resourceMap.getString("jPanel2.TabConstraints.tabTitle"), jPanel2); // NOI18N

        jPanel3.setName("jPanel3"); // NOI18N

        jScrollPane3.setName("jScrollPane3"); // NOI18N

        productCategoriesList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        productCategoriesList.setName("productCategoriesList"); // NOI18N
        productCategoriesList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                productCategoriesListValueChanged(evt);
            }
        });
        jScrollPane3.setViewportView(productCategoriesList);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE)
        );

        categories.addTab(resourceMap.getString("jPanel3.TabConstraints.tabTitle"), jPanel3); // NOI18N

        newCategoryTextField.setText(resourceMap.getString("newCategoryTextField.text")); // NOI18N
        newCategoryTextField.setName("newCategoryTextField"); // NOI18N

        addCategoryButton.setText(resourceMap.getString("addCategoryButton.text")); // NOI18N
        addCategoryButton.setName("addCategoryButton"); // NOI18N
        addCategoryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addCategoryButtonActionPerformed(evt);
            }
        });

        deleteCategoryButton.setText(resourceMap.getString("deleteCategoryButton.text")); // NOI18N
        deleteCategoryButton.setName("deleteCategoryButton"); // NOI18N
        deleteCategoryButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteCategoryButtonActionPerformed(evt);
            }
        });

        btnEdytuj.setText(resourceMap.getString("btnEdytuj.text")); // NOI18N
        btnEdytuj.setName("btnEdytuj"); // NOI18N
        btnEdytuj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEdytujActionPerformed(evt);
            }
        });
        btnEdytuj.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                btnEdytujAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        jTextFieldEdytuj.setText(resourceMap.getString("jTextFieldEdytuj.text")); // NOI18N
        jTextFieldEdytuj.setName("jTextFieldEdytuj"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(categories, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(newCategoryTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                    .addComponent(addCategoryButton, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                    .addComponent(deleteCategoryButton, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                    .addComponent(btnEdytuj, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE)
                    .addComponent(jTextFieldEdytuj, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(categories, javax.swing.GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(33, 33, 33)
                        .addComponent(newCategoryTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(addCategoryButton)
                        .addGap(18, 18, 18)
                        .addComponent(jTextFieldEdytuj, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(3, 3, 3)
                        .addComponent(btnEdytuj)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteCategoryButton)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addCategoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addCategoryButtonActionPerformed
        String nazwaKategorii="";
        DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());
        switch (categories.getSelectedIndex()) {
            case 0:
                nazwaKategorii = newCategoryTextField.getText();
                baza.dodajKategorieWydatku(nazwaKategorii);

                //aktualizacja danych listy               
                expensesCategoriesList.setListData(baza.listaKategoriaWydatek().toArray());
                
                break;
            case 1:
                nazwaKategorii = newCategoryTextField.getText();
                 baza.dodajKategoriePrzychodu(nazwaKategorii);
                //aktualizacja danych listy                
                contributionCategoriesList.setListData(baza.listaKategoriaPrzychod().toArray());
                break;
            case 2:
                nazwaKategorii = newCategoryTextField.getText();
                baza.dodajKategorieProduktu(nazwaKategorii);                
                productCategoriesList.setListData(baza.listaKategoriaProdukt().toArray());
                break;
        }
        baza.close();
        newCategoryTextField.setText("");
    }//GEN-LAST:event_addCategoryButtonActionPerformed

    private void deleteCategoryButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteCategoryButtonActionPerformed
        String nazwaKategorii="";
        DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());

        switch (categories.getSelectedIndex()) {
            case 0:
                nazwaKategorii = (String)expensesCategoriesList.getSelectedValue();
                 baza.usunKategorieWydatku(nazwaKategorii);
               
               //aktualizacja danych listy                
                expensesCategoriesList.setListData(baza.listaKategoriaWydatek().toArray());

                if (expensesCategoriesList.getSelectedIndex() == -1) {
                    deleteCategoryButton.setVisible(false);
                    btnEdytuj.setVisible(false);
                    jTextFieldEdytuj.setVisible(false);
                } else {
                    deleteCategoryButton.setVisible(true);
                    btnEdytuj.setVisible(true);
                    jTextFieldEdytuj.setVisible(true);
                }
                break;
            case 1:
                nazwaKategorii = (String)contributionCategoriesList.getSelectedValue();
                baza.usunKategoriePrzychodu(nazwaKategorii);

               //aktualizacja danych listy                
                contributionCategoriesList.setListData(baza.listaKategoriaPrzychod().toArray());
                if (contributionCategoriesList.getSelectedIndex() == -1) {
                    deleteCategoryButton.setVisible(false);
                    btnEdytuj.setVisible(false);
                    jTextFieldEdytuj.setVisible(false);
                } else {
                    deleteCategoryButton.setVisible(true);
                    btnEdytuj.setVisible(true);
                    jTextFieldEdytuj.setVisible(true);
                }
                break;
            case 2:
                nazwaKategorii = (String)productCategoriesList.getSelectedValue();
                baza.usunKategorieProduktu(nazwaKategorii);
                //aktualizacja danych listy
                
                productCategoriesList.setListData(baza.listaKategoriaProdukt().toArray());
                if (productCategoriesList.getSelectedIndex() == -1) {
                    deleteCategoryButton.setVisible(false);
                    btnEdytuj.setVisible(false);
                    jTextFieldEdytuj.setVisible(false);
                } else {
                    deleteCategoryButton.setVisible(true);
                    btnEdytuj.setVisible(true);
                    jTextFieldEdytuj.setVisible(true);
                }
                break;
        }
        baza.close();
    }//GEN-LAST:event_deleteCategoryButtonActionPerformed

    private void contributionCategoriesListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_contributionCategoriesListValueChanged
        if (contributionCategoriesList.getSelectedIndex() == -1) {
            deleteCategoryButton.setVisible(false);
            btnEdytuj.setVisible(false);
            jTextFieldEdytuj.setVisible(false);
        } else {
            deleteCategoryButton.setVisible(true);
            btnEdytuj.setVisible(true);
            jTextFieldEdytuj.setVisible(true);
            jTextFieldEdytuj.setText((String)contributionCategoriesList.getSelectedValue());
        }
    }//GEN-LAST:event_contributionCategoriesListValueChanged

    private void categoriesStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_categoriesStateChanged
        switch (categories.getSelectedIndex()) {
            case 0:
                if (expensesCategoriesList.getSelectedIndex() == -1) {
                    deleteCategoryButton.setVisible(false);
                    btnEdytuj.setVisible(false);
                    jTextFieldEdytuj.setVisible(false);
                } else {
                    deleteCategoryButton.setVisible(true);
                    btnEdytuj.setVisible(true);
                    jTextFieldEdytuj.setVisible(true);
                }
                break;
            case 1:
                if (contributionCategoriesList.getSelectedIndex() == -1) {
                    deleteCategoryButton.setVisible(false);
                    btnEdytuj.setVisible(false);
                    jTextFieldEdytuj.setVisible(false);
                } else {
                    deleteCategoryButton.setVisible(true);
                    btnEdytuj.setVisible(true);
                    jTextFieldEdytuj.setVisible(true);
                }
                break;
        }
}//GEN-LAST:event_categoriesStateChanged

    private void btnEdytujAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_btnEdytujAncestorAdded
        // TODO add your handling code here:
    }//GEN-LAST:event_btnEdytujAncestorAdded

    private void btnEdytujActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEdytujActionPerformed
        // TODO add your handling code here:
        String nazwaKategorii="";
        String nowaNazwaKategorii ="";
        DataBaseMySql baza = new DataBaseMySql(HomeBudgetApp.getConnectionString());

        switch (categories.getSelectedIndex()) {
            case 0:
                nazwaKategorii = (String)expensesCategoriesList.getSelectedValue();
                nowaNazwaKategorii = jTextFieldEdytuj.getText();
                baza.edytujKategorieWydatku(nazwaKategorii, nowaNazwaKategorii);
                //aktualizacja danych listy                
                expensesCategoriesList.setListData(baza.listaKategoriaWydatek().toArray());

                if (expensesCategoriesList.getSelectedIndex() == -1) {
                    deleteCategoryButton.setVisible(false);
                } else {
                    deleteCategoryButton.setVisible(true);
                }
                break;
            case 1:
                nazwaKategorii = (String)contributionCategoriesList.getSelectedValue();
                nowaNazwaKategorii = jTextFieldEdytuj.getText();
                baza.edytujKategoriePrzychodu(nazwaKategorii, nowaNazwaKategorii);
               //aktualizacja danych listy                
                contributionCategoriesList.setListData(baza.listaKategoriaPrzychod().toArray());
                if (contributionCategoriesList.getSelectedIndex() == -1) {
                    deleteCategoryButton.setVisible(false);
                    btnEdytuj.setVisible(false);
                } else {
                    deleteCategoryButton.setVisible(true);
                    btnEdytuj.setVisible(true);
                }
                break;
            case 2:
                nazwaKategorii = (String)productCategoriesList.getSelectedValue();
                nowaNazwaKategorii = jTextFieldEdytuj.getText();
                baza.edytujKategorieProduktu(nazwaKategorii, nowaNazwaKategorii);
               //aktualizacja danych listy                
                productCategoriesList.setListData(baza.listaKategoriaProdukt().toArray());
                if (productCategoriesList.getSelectedIndex() == -1) {
                    deleteCategoryButton.setVisible(false);
                    btnEdytuj.setVisible(false);
                } else {
                    deleteCategoryButton.setVisible(true);
                    btnEdytuj.setVisible(true);
                }
                break;

        }
        baza.close();
    }//GEN-LAST:event_btnEdytujActionPerformed

    private void productCategoriesListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_productCategoriesListValueChanged
        // TODO add your handling code here:
        if (productCategoriesList.getSelectedIndex() == -1) {
            deleteCategoryButton.setVisible(false);
            btnEdytuj.setVisible(false);
            jTextFieldEdytuj.setVisible(false);
        } else {
            deleteCategoryButton.setVisible(true);
            btnEdytuj.setVisible(true);
            jTextFieldEdytuj.setVisible(true);
            jTextFieldEdytuj.setText((String)productCategoriesList.getSelectedValue());
        }
    }//GEN-LAST:event_productCategoriesListValueChanged

    private void expensesCategoriesListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_expensesCategoriesListValueChanged
        if (expensesCategoriesList.getSelectedIndex() == -1) {
            deleteCategoryButton.setVisible(false);
            btnEdytuj.setVisible(false);
            jTextFieldEdytuj.setVisible(false);
        } else {
            deleteCategoryButton.setVisible(true);
            btnEdytuj.setVisible(true);
            jTextFieldEdytuj.setVisible(true);
            jTextFieldEdytuj.setText((String)expensesCategoriesList.getSelectedValue());
        }
}//GEN-LAST:event_expensesCategoriesListValueChanged

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                CategoriesDialog dialog = new CategoriesDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addCategoryButton;
    private javax.swing.JButton btnEdytuj;
    private javax.swing.JTabbedPane categories;
    private javax.swing.JList contributionCategoriesList;
    private javax.swing.JButton deleteCategoryButton;
    private javax.swing.JList expensesCategoriesList;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField jTextFieldEdytuj;
    private javax.swing.JTextField newCategoryTextField;
    private javax.swing.JList productCategoriesList;
    // End of variables declaration//GEN-END:variables
}
